/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import clases.TipoPago;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author root
 */
public class MdlTipoPago implements Modelo<TipoPago>{

    /**
     * 
     * @param tipopago
     * @param cnx
     * @throws SQLException 
     */
    @Override
    public void insert(TipoPago tipopago, Conexion cnx) throws SQLException {
        String sql = "INSERT INTO tipoPago(nombre) "
                + "VALUES ('" +tipopago.getNombre() + "');";
        cnx.ejecutar(sql);
    }

    /**
     * 
     * @param cnx
     * @return
     * @throws SQLException 
     */
    @Override
    public int getLastId(Conexion cnx) throws SQLException {
        String sql = "SEECT MAX(idTipoPago) FROM tipoPAgo;";
        ResultSet rs = cnx.consultar(sql);
        if (rs.next()) {
            return rs.getInt(1);
        }
        return -1;
    }

    /**
     * 
     * @param tipopago
     * @param cnx
     * @throws SQLException 
     */
    @Override
    public void update(TipoPago tipopago, Conexion cnx) throws SQLException {
        String sql = "UPDATE tipoPago SET "
                + "nombre = '" + tipopago.getNombre() + "' "
                + "WHERE idTipoPago = " + tipopago.getIdTipoPago();
        cnx.ejecutar(sql);
    }

    /**
     * 
     * @param query
     * @param cnx
     * @return
     * @throws SQLException 
     */
    @Override
    public ArrayList<TipoPago> get(String query, Conexion cnx) throws SQLException {
        ResultSet rs = cnx.consultar(query);
        ArrayList<TipoPago> tipos = new ArrayList<>();
        while(rs.next()){
            tipos.add(new TipoPago(rs.getInt(1), rs.getString(2)));
        }
        return tipos;
    }
    
    /**
     * 
     * @param id
     * @param cnx
     * @return
     * @throws SQLException 
     */
    @Override
    public TipoPago get(int id, Conexion cnx) throws SQLException {
        String sql = "SELECT * FROM tipoPago WHERE idTipoPago LIKE " + id;
        ResultSet rs = cnx.consultar(sql);
        TipoPago tipo = null;
        if (rs.next()) {
            tipo = new TipoPago(rs.getInt(1), rs.getString(2));
        }
        return tipo;
    }
    
}
